﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
using ClipShow2.MediaFinder;
using ClipShow2.MediaTypes;
using ClipShow2.Storage;

namespace ClipShow2.LibraryManager
{
    public class NotifierEventArgs : EventArgs
    {
        private ClipShowContent _eventLibrary;

        public NotifierEventArgs(ClipShowContent eventLibrary)
        {
            _eventLibrary = eventLibrary;
        }

        public ClipShowContent NotifierLibrary
        {
            get { return(_eventLibrary);}
        }
    }
    public delegate void NotifierEventHandler(object sender, NotifierEventArgs e);

    /// <summary>
    /// Exception notification class
    /// </summary>
    public class NotifierExceptionArgs : EventArgs
    {
        private string _errText;

        public NotifierExceptionArgs(string errText)
        {
            _errText = errText;
        }

        public string NotifierExceptionText
        {
            get
            {
                return (_errText);
            }
        }
    }

    public delegate void NotifierExceptionHandler(object sender, NotifierExceptionArgs e);

    public class Notifier
    {
        public Notifier()
        {

        }

        public event NotifierEventHandler NotifierAlert;
        public event NotifierExceptionHandler NotifierException;
        protected virtual void OnNotifierAlert(NotifierEventArgs e)
        {
            if (NotifierAlert != null)
            {
                // Invokes the delegates. 
                NotifierAlert(this, e);
            }
        }
        protected virtual void OnNotifierExceptionAlert(NotifierExceptionArgs e)
        {
            if (NotifierException != null)
            {
                // Invokes the delegates. 
                NotifierException(this, e);
            }
        }

        public void Start()
        {
            Thread _localMedia;
            _localMedia = new Thread(new ThreadStart(CollectLocalMedia));
            _localMedia.Name = "vmc_ClipShow_LocalMediaCollection";
            _localMedia.IsBackground = true;
            _localMedia.Priority = ThreadPriority.Normal;
            _localMedia.Start();
        }

        public void BuildThumbnails()
        {
            StorageEngine.Initialize();
            ClipShowContent csc = StorageEngine.GetMediaLibrary(StorageEngine.MediaLibraryPath("my media"));
            try
            {
                foreach (ClipShowMetadata item in csc.MediaItems)
                {
                    if (!File.Exists(item.ThumbnailUrl))
                    {
                        ThumbnailCreator t = new ThumbnailCreator();
                        System.Drawing.Bitmap thumb = t.GetThumbNail(item.Url);
                        thumb.Save(item.ThumbnailUrl);
                    }
                }
            }
            catch (SystemException se)
            {
                throw se;
            }
        }

        /// <summary>
        /// Searches for media and build thumbnails if needed
        /// </summary>
        private void CollectLocalMedia()
        {
            try
            {
                StorageEngine.Initialize();
                ClipShowContent csc = SearchEngine.SearchLocalCatalogAll("my media");
                StorageEngine.PersistMediaLibrary(csc);
                
                // Fire event notifying delegates that the media file is now ready
                NotifierEventArgs e = new NotifierEventArgs(csc);
                OnNotifierAlert(e);
            }
            catch (SystemException se)
            {
                // Fire exception event notifying delegates that something has gone wrong
                NotifierExceptionArgs err = new NotifierExceptionArgs(se.Message);
                OnNotifierExceptionAlert(err);
            }
        }
    }
}
